﻿using System;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YJKAxisAndEdit
{
	public class MainAuxiliaryConversion
	{
		public MainAuxiliaryConversion(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public Result Convert()
		{
			Transaction transaction = null;
			Result result;
			try
			{
				Document document = this.m_Revit.Application.ActiveUIDocument.Document;
				transaction = new Transaction(document);
				transaction.Start("main/auxiliary conversion");
				Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
				Element element = document.GetElement(selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new GridFilter(), "选择轴线"));
				Grid grid = element as Grid;
				if (grid == null)
				{
					throw new System.ArgumentNullException("不是轴线");
				}
				AxisNameManager axisNameManager = new AxisNameManager(this.m_Revit);
				string text = "";
				if (!axisNameManager.MainAuxiliaryConversion(grid.Name, ref text))
				{
					if (text.CompareTo("") != 0)
					{
						MessageBox.Show(text, "盈建科提示");
					}
					if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
					{
						transaction.RollBack();
					}
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception)
			{
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		protected ExternalCommandData m_Revit;
	}
}
